package com.example.project.mapper;

import java.util.List;
import org.apache.ibatis.annotations.*;
import com.example.project.entity.Journal;

/**
 * 问题变更历史主，记录Issue等对象的变更信息Mapper接口
 * 
 * @author ruoyi
 * @date 2025-09-26
 */
@Mapper
public interface JournalMapper {
	
	/**
	 * 查询问题变更历史主，记录Issue等对象的变更信息
	 * 
	 * @param id 问题变更历史主，记录Issue等对象的变更信息主键
	 * @return 问题变更历史主，记录Issue等对象的变更信息
	 */
	@Select("select id, journalized_id, journalized_type, user_id, notes, private_notes, created_on, updated_on, updated_by_id from t_journal where id = #{id}")
	public Journal selectJournalsById(Long id);

	/**
	 * 查询问题变更历史主，记录Issue等对象的变更信息列表
	 * 
	 * @param tJournals 问题变更历史主，记录Issue等对象的变更信息
	 * @return 问题变更历史主，记录Issue等对象的变更信息集合
	 */
	@Select("<script>" +
			"select id, journalized_id, journalized_type, user_id, notes, private_notes, created_on, updated_on, updated_by_id from t_journal" +
			"<where>" +
			"<if test='journalizedId != null'> and journalized_id = #{journalizedId}</if>" +
			"<if test='journalizedType != null and journalizedType != \"\"'> and journalized_type = #{journalizedType}</if>" +
			"<if test='userId != null'> and user_id = #{userId}</if>" +
			"<if test='notes != null and notes != \"\"'> and notes = #{notes}</if>" +
			"<if test='privateNotes != null'> and private_notes = #{privateNotes}</if>" +
			"<if test='createdOn != null'> and created_on = #{createdOn}</if>" +
			"<if test='updatedOn != null'> and updated_on = #{updatedOn}</if>" +
			"<if test='updatedById != null'> and updated_by_id = #{updatedById}</if>" +
			"</where>" +
			"</script>")
	public List<Journal> selectJournalsList(Journal tJournals);

	/**
	 * 新增问题变更历史主，记录Issue等对象的变更信息
	 * 
	 * @param tJournals 问题变更历史主，记录Issue等对象的变更信息
	 * @return 结果
	 */
	@Insert("<script>" +
			"insert into t_journal" +
			"<trim prefix='(' suffix=')' suffixOverrides=','>" +
			"<if test='journalizedId != null'>journalized_id,</if>" +
			"<if test='journalizedType != null and journalizedType != \"\"'>journalized_type,</if>" +
			"<if test='userId != null'>user_id,</if>" +
			"<if test='notes != null'>notes,</if>" +
			"<if test='privateNotes != null'>private_notes,</if>" +
			"<if test='createdOn != null'>created_on,</if>" +
			"<if test='updatedOn != null'>updated_on,</if>" +
			"<if test='updatedById != null'>updated_by_id,</if>" +
			"</trim>" +
			"<trim prefix='values (' suffix=')' suffixOverrides=','>" +
			"<if test='journalizedId != null'>#{journalizedId},</if>" +
			"<if test='journalizedType != null and journalizedType != \"\"'>#{journalizedType},</if>" +
			"<if test='userId != null'>#{userId},</if>" +
			"<if test='notes != null'>#{notes},</if>" +
			"<if test='privateNotes != null'>#{privateNotes},</if>" +
			"<if test='createdOn != null'>#{createdOn},</if>" +
			"<if test='updatedOn != null'>#{updatedOn},</if>" +
			"<if test='updatedById != null'>#{updatedById},</if>" +
			"</trim>" +
			"</script>")
	@Options(useGeneratedKeys = true, keyProperty = "id")
	public int insertJournals(Journal tJournals);

	/**
	 * 修改问题变更历史主，记录Issue等对象的变更信息
	 * 
	 * @param tJournals 问题变更历史主，记录Issue等对象的变更信息
	 * @return 结果
	 */
	@Update("<script>" +
			"update t_journal" +
			"<trim prefix='SET' suffixOverrides=','>" +
			"<if test='journalizedId != null'>journalized_id = #{journalizedId},</if>" +
			"<if test='journalizedType != null and journalizedType != \"\"'>journalized_type = #{journalizedType},</if>" +
			"<if test='userId != null'>user_id = #{userId},</if>" +
			"<if test='notes != null'>notes = #{notes},</if>" +
			"<if test='privateNotes != null'>private_notes = #{privateNotes},</if>" +
			"<if test='createdOn != null'>created_on = #{createdOn},</if>" +
			"<if test='updatedOn != null'>updated_on = #{updatedOn},</if>" +
			"<if test='updatedById != null'>updated_by_id = #{updatedById},</if>" +
			"</trim>" +
			"where id = #{id}" +
			"</script>")
	public int updateJournals(Journal tJournals);

	/**
	 * 删除问题变更历史主，记录Issue等对象的变更信息
	 * 
	 * @param id 问题变更历史主，记录Issue等对象的变更信息主键
	 * @return 结果
	 */
	@Delete("delete from t_journal where id = #{id}")
	public int deleteJournalsById(Long id);

	/**
	 * 批量删除问题变更历史主，记录Issue等对象的变更信息
	 * 
	 * @param ids 需要删除的数据主键集合
	 * @return 结果
	 */
	@Delete("<script>" +
			"delete from t_journal where id in " +
			"<foreach item='id' collection='array' open='(' separator=',' close=')'>" +
			"#{id}" +
			"</foreach>" +
			"</script>")
	public int deleteJournalsByIds(String[] ids);
	
	@Select("select id, journalized_id, journalized_type, user_id, notes, private_notes, "
			+ "created_on, updated_on, updated_by_id from t_journal where journalized_id = #{journalizedId}")
	public List<Journal> selectJournalsByJournalizedId(Long journalizedId);

	/**
	 * 根据journalized_id查询最后一条更新记录
	 * 
	 * @param journalizedId 关联的ID
	 * @return 最后一条记录
	 */
	@Select("select id, journalized_id, journalized_type, user_id, notes, private_notes, created_on, updated_on, updated_by_id from t_journal where journalized_id = #{journalizedId} order by updated_on desc limit 1")
	public Journal selectLastJournalByJournalizedId(Long journalizedId);
} 
